package com.genius.reservation.common.rabbitmq;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class RabbitConfig {


    public static final String RECEIVE_MANAGER = "receive_manager";
    public static final String DRIVER = "driver";
    public static final String RECEIVER = "receiver";
    public static final String SUPPLIER = "supplier";

    public static final String DIRECT_EXCHANGE = "directExchange";
    public static final String DIRECT_ROUTING_KEY = "direct";
//@Autowired
//    private RabbitTemplate rabbitTemplate;
//
//@PostConstruct
//public void init(){
//    rabbitTemplate.setMessageConverter(new FastJsonMessageConverter());
//}
//    /**
//     * 配置接收的消息转换器
//     * @return
//     */
//    @Bean
//    public MessageConverter jsonMessageConverter() {
//        return new FastJsonMessageConverter();
//    }

    @Bean(value = "receiveManager")
    public Queue receiveManagerQueue(){

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("rabbitmq_simple",true,true,false);
        return new Queue(RECEIVE_MANAGER,true,false,false);
    }
    @Bean(value = "driver")
    public Queue driverQueue(){

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("rabbitmq_simple",true,true,false);
        return new Queue(DRIVER,true,false,false);
    }

    @Bean(value = "receiver")
    public Queue receiverQueue(){

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("rabbitmq_simple",true,true,false);
        return new Queue(RECEIVER,true,false,false);
    }

    @Bean(value = "supplier")
    public Queue supplierQueue(){

        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("rabbitmq_simple",true,true,false);
        return new Queue(SUPPLIER,true,false,false);
    }

    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE, true, false);
    }

    @Bean
    public Binding bindingDirectExchange(@Qualifier(value ="receiveManager" ) Queue directQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(DIRECT_ROUTING_KEY);
    }
    @Bean
    public Binding bindingDirectExchange1(@Qualifier(value ="driver" ) Queue directQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(DIRECT_ROUTING_KEY);
    }
    @Bean
    public Binding bindingDirectExchange2(@Qualifier(value ="receiver" ) Queue directQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(DIRECT_ROUTING_KEY);
    }
    @Bean
    public Binding bindingDirectExchange3(@Qualifier(value ="supplier" ) Queue directQueue, DirectExchange directExchange) {
        return BindingBuilder.bind(directQueue).to(directExchange).with(DIRECT_ROUTING_KEY);
    }

}
